var gulp = require('gulp');
var git = require('gulp-git');
var zip = require('gulp-zip');
var merge = require('merge-stream');
var del = require('del');
var vinylPaths = require('vinyl-paths');
var Q = require('q');
var package = require('./package.json');
var destDir = './target/';

gulp.task("clean",function(){
    return gulp.src(destDir).pipe(vinylPaths(del));

});




gulp.task('electron_prebuilt',['clean'],function(){
   
    return merge(gulp.src("./node_modules/electron-prebuilt/dist/*")
	.pipe(gulp.dest(destDir+"package/"))
    ,

     
gulp.src("./node_modules/electron-prebuilt/dist/**/*")
	.pipe(gulp.dest(destDir+"package/"))
	,
	 gulp.src(["./run.bat","./index.html","./main.js"]).pipe(gulp.dest(destDir+"package/"))
		
	 ,	gulp.src("./dist/**")
    .pipe(gulp.dest(destDir+"package/dist"))
	 
	
	,
	
		gulp.src("./assets/*")
    .pipe(gulp.dest(destDir+"package/assets"))
	,
	
		gulp.src("./assets/**/*")
    .pipe(gulp.dest(destDir+"package/assets"))


	);

})
var rev=""
gulp.task('getVersion',['electron_prebuilt'] ,function(){
    var deferred = Q.defer();
	
	git.revParse({args:'--short HEAD'}, function (err, hash) {
  //if (err) ... 
        console.log('current git hash: '+hash);
        rev =hash
		deferred.resolve();
    });
    return deferred.promise;
});


gulp.task('default', ['getVersion'],function(){
console.log("rec=",rev)
return gulp.src([destDir+"package/*",destDir+"package/**/*"])
        .pipe(zip('bilibili-vue2-electron-'+rev+'.zip'))
        .pipe(gulp.dest(destDir));
});